//线路报价管理

interface ILinePriceManagentDetailViewModel {
    /**
     * 编号
     */
    id?: string;
    /**
     * 启用时间
     */
    openTime: string;
    /**
     * 关闭时间
     */
    closeTime: string;
    /**
     * 起始省编号
     */
    startProvinceCode: string;
    /**
     * 起始地市编号
     */
    startCityCode: string;
    /**
     * 起始地区编号
     */
    startAreaCode: string;
    /**
     * 目的地省编号
     */
    endProvinceCode: string;
    /**
     * 目的地市编号
     */
    endCityCode: string;
    /**
     * 目的地区编号
     */
    endAreaCode: string;
    /**
     * 吨数范围
     */
    tonnageRange: string;
    /**
     * 单价
     */
    price: number;
    /**
     * 单价单位
     */
    priceUnit: string;
    /**
     * 货物类别编号
     */
    goodsTypeId: string;
    /**
     * 货物分类名称
     */
    goodsTypeName?: string;
    /**
     * 货物编号
     */
    goodsId: string;
    /**
     * 货物名称
     */
    goodsName?: string;
    /**
     * 装车方式
     */
    loadingType: string;
    /**
     * 车长
     */
    carLength: string;
    /**
     * 结算方式
     */
    setleWay: string;
    /**
     * 报价类型
     */
    priceType: string;
    /**
     * 公里数
     */
    mileage: number;
    /**
     * 报价状态
     */
    priceState: string;
    /**
     * 结算放名称
     */
    settleName: string;
    /**
     * 结算方手机号
     */
    settlePhoneNumber: string;
    /**
     * 结算方联系人
     */
    settleContact: string;
    /**
     * 备注
     */
    remarks: string;
    /**
     * 点击事件
     */
    click: () => void;
}

interface ILinePriceManagentViewModel extends IBaseViewModel<ILinePriceManagentQueryTerms> {
    /**
     * 时间日期格式
     */
    dateFormat: "{format:'YYYY.MM.DD HH:mm'}";
    /**
     * ui-grid控制
     */
    gridOptions: uiGrid.IGridOptionsOf<LinePriceListItemResponse>;
    /**
     * 价格状态下拉
     */
    priceStateDropDown: ValueListItemResponse[];
    /**
     * 货物类别列表下拉
     */
    goodsTypeDropDown: GoodsTypeListItemResponse[];
    /**
     * 货物名称填充
     */
    changeGoods: () => void;
    goodsNameList: GoodsListItemResponse[];
    /**
     * 结算类型下拉
     */
    settleWayDropDown: ValueListItemResponse[];
    /**
     * 装车方式下拉
     */
    loadingTypeDropDown: ValueListItemResponse[];
    /**
     * 报价类型下拉
     */
    priceTypeDropDown: ValueListItemResponse[];
    /**
     * 单价单位下拉
     */
    priceUnitDropDown: ValueListItemResponse[];
    /**
     * 车长下拉
     */
    carLengthDropDown: ValueListItemResponse[];
    /**
     * 添加/编辑详情
     */
    detail: ILinePriceManagentDetailViewModel;
    /**
     * 添加线路报价
     */
    addLinePrice: () => void;
    /**
     * 编辑线路报价
     */
    editLinePrice: (id: string) => void;
    /**
     * 删除线路报价
     */
    deleteLinePrice: (id: string) => void;
    /**
     * 监听结算单位自动完成事件
     */
    onSettleComplete: (value: string) => any;
    /**
     * 监听选中结算单位事件
     */
    onSelectSettle: (element: any) => void;
    /**
     * 客户单位名称
     */
    clientName: string;
    /**
     * 列表高
     */
    heightA: number;

    startCityList: Array<any>;
    startAreaList: Array<any>;
    endCityList: Array<any>;
    endAreaList: Array<any>;
    /**
     * 是否自动选中省市区
     */
    ischecked: boolean;
}

interface ILinePriceManagentQueryTerms {
    /**
     * 价格状态
     */
    priceState: string;
    /**
     * 所属货物类别
     */
    goodsTypeId: string;
    /**
     * 起始地
     */
    beginAddress: string;
    /**
     * 目的地
     */
    endAddress: string;
}

class LinePriceManagentController extends ListController<ILinePriceManagentViewModel, ILinePriceManagentQueryTerms> {
    gridApi: uiGrid.IGridApiOf<LinePriceListItemResponse>;
    enterpriseId: string;

    constructor($scope: ILinePriceManagentViewModel, private linePriceService: ILinePriceService, private settleService: ISettleService, private areaService: IAreaService, private goodsService: IGoodsService,
        private goodsTypeService: IGoodsTypeService, private valueService: IValueService, private $ngBootbox: BootboxService, private $location: ng.ILocationService) {
        super($scope);
        this.init();
    }

    init(): void {
        super.init();
        //this.enterpriseId = this.$location.search().id;

        this.enterpriseId = JSON.parse(window.localStorage.getItem("client").toString()).clientId;
        this.$scope.clientName = JSON.parse(window.localStorage.getItem("client").toString()).clientName;

        this.$scope.addLinePrice = this.showAddLinePriceDialog;
        this.$scope.editLinePrice = this.showEditLinePriceDialog;
        this.$scope.onSettleComplete = this.autoCompleteSettle;
        this.$scope.onSelectSettle = this.selectSettle;
        this.$scope.deleteLinePrice = this.deleteLinePrice;

        this.$scope.ischecked = false;

        //this.$scope.changeGoods = this.changeGoods;

        this.$scope.queryTerms = {
            priceState: "",
            goodsTypeId: "",
            beginAddress: "",
            endAddress: ""
        };

        this.$scope.gridOptions = {
            enableSorting: false,
            useExternalPagination: true,
            useExternalSorting: true,
            enableColumnResizing: true,
            paginationPageSizes: [10, 20, 30],
            columnDefs: [
                { name: "序号", field: "index", enableColumnMenu: false },
                { name: "起始地", field: "beginAddress", enableColumnMenu: false },
                { name: "目的地", field: "endAddress", enableColumnMenu: false },
                { name: "吨位范围", field: "tonnageRange", enableColumnMenu: false },
                { name: "货物类别", field: "goodTypeName", enableColumnMenu: false },
                { name: "车长", field: "carLength", enableColumnMenu: false },
                { name: "线路报价", field: "price", enableColumnMenu: false },
                { name: "价格状态", field: "priceState", enableColumnMenu: false },
                {
                    name: "操作",
                    cellFilter: 'checkmark',
                    cellTemplate: '<a data-toggle="modal" title="编辑" data-target="#lineprice" class="fa fa-edit m-l-xs primary text-info" ng-click="grid.appScope.editLinePrice(row.entity.id)"></a> <a class="primary fa fa-trash-o text-danger" title="删除" ng-click="grid.appScope.deleteLinePrice(row.entity.id)"></a>',
                    enableColumnMenu: false
                }
            ],
            onRegisterApi: (gridApi) => {
                this.gridApi = gridApi;
                gridApi.pagination.on.paginationChanged(this.$scope, this.paginationChanged);
            }
        }
        this.query();
        this.loadGoodsTypeDropDown();
        this.loadPriceStateDropDown();
        this.loadSettleWayDropDown();
        this.loadLoadingTypeDropDown();
        this.loadPriceTypeDropDown();
        this.loadPriceUnitDropDown();
        this.loadCarLengthDropDown();
    }

    loadData(queryTerms: ILinePriceManagentQueryTerms, skip: number, count: number): void {
        this.linePriceService.getList(this.enterpriseId, queryTerms.goodsTypeId, queryTerms.priceState, "", "", "", "", "", "", "",
            "", queryTerms.beginAddress, queryTerms.endAddress, skip, count).then(result => {
                this.$scope.gridOptions.totalItems = result.data.total;
                this.$scope.gridOptions.data = result.data.data;
                result.data.data.map((item, index) => item.index = skip++ + 1);
                this.$scope.heightA = (this.$scope.gridOptions.data.length * 39) + 95;

                if (skip == 0) {
                    this.$scope.gridOptions.paginationCurrentPage = 1;
                }
                this.$scope.querying = false;
            }, error => {
                this.$scope.querying = false;
            });
    }

    /**
     * 加载先乱报价状态下拉
     */
    loadPriceStateDropDown(): void {
        this.$scope.priceStateDropDown = this.valueService.getLinePriceStateList().data;
    }

    /**
     * 加载货物类别下拉
     */
    loadGoodsTypeDropDown(): void {
        this.goodsTypeService.getList(this.enterpriseId, "", 0, -1).then(result => {
            this.$scope.goodsTypeDropDown = result.data.data;
        });
        this.$scope.changeGoods = this.changeGoods;
    }

    /**
     * 加载货物名称
     */
    changeGoods = () => {
        this.goodsService.getList(this.enterpriseId, "", this.$scope.detail.goodsTypeId, 0, -1).then((res) => {
            this.$scope.goodsNameList = res.data.data;
            if (this.$scope.detail.goodsTypeId == undefined) {
                this.$scope.goodsNameList == null;
            }
        });
    }

    /**
     * 加载结算方式下拉
     */
    loadSettleWayDropDown(): void {
        this.$scope.settleWayDropDown = this.valueService.getSettlementTypeList().data;
    }

    /**
     * 加载装车方式下拉
     */
    loadLoadingTypeDropDown(): void {
        this.$scope.loadingTypeDropDown = this.valueService.getCarrierWayList().data;
    }

    /**
     * 加载报价类型下拉
     */
    loadPriceTypeDropDown(): void {
        this.$scope.priceTypeDropDown = this.valueService.getLinePriceTypeList().data;
    }

    /**
     * 加载单价单位下拉
     */
    loadPriceUnitDropDown(): void {
        this.$scope.priceUnitDropDown = this.valueService.getPriceUnitList().data;
    }

    /**
     * 加载车场下拉
     */
    loadCarLengthDropDown(): void {
        this.$scope.carLengthDropDown = this.valueService.getCarLengthList().data;
    }

    /**
     * -----------添加/编辑时结算方自动补全
     */
    autoCompleteSettle = (name: string) => {
        return this.settleService.getList(this.enterpriseId, name, "", true, 0, 5).then(result => {
            return result.data.data;
        });
    }

    selectSettle = (element: any) => {
        if (element) {
            this.$scope.detail.settlePhoneNumber = element.description.phoneNumber;
            this.$scope.detail.settleContact = element.description.contactMan;
        }
    }
    /**
     * -----------结束
     */

    showAddLinePriceDialog = () => {
        this.$scope.ischecked = false;
        this.$scope.title = "添加线路报价";
        this.$scope.startCityList = [];
        this.$scope.startAreaList = [];
        this.$scope.endCityList = [];
        this.$scope.endAreaList = [];
        this.$scope.detail = {
            openTime: "",
            closeTime: "",
            startProvinceCode: "",
            startCityCode: "",
            startAreaCode: "",
            endProvinceCode: "",
            endCityCode: "",
            endAreaCode: "",
            tonnageRange: "",
            price: null,
            priceUnit: "",
            goodsTypeId: "",
            goodsTypeName: "",
            loadingType: "",
            carLength: "",
            setleWay: "",
            priceType: "",
            mileage: null,
            priceState: "",
            settleName: "",
            settleContact: "",
            settlePhoneNumber: "",
            remarks: "",
            goodsId: "",
            goodsName: "",
            click: this.addLinePrice
        };
        this.$scope.goodsNameList = null;
        this.$scope.$broadcast("angucomplete-alt:clearInput");
        $('#opendataTime').click(function () {
            $('#dataTime').datetimepicker('show');
        });
        $('#opendataTimeEnd').click(function () {
            $('#dataTimeEnd').datetimepicker('show');
        });
    }

    addLinePrice = () => {
        if (!$("#lineProceForm").valid()) { return }
        this.$ngBootbox.confirm("确定保存此数据吗？").then(e => {
            this.linePriceService.add(
                this.enterpriseId,
                this.$scope.detail.openTime,
                this.$scope.detail.goodsId,
                this.$scope.detail.closeTime,
                this.$scope.detail.startProvinceCode,
                this.$scope.detail.startCityCode,
                this.$scope.detail.startAreaCode,
                this.$scope.detail.endProvinceCode,
                this.$scope.detail.endCityCode,
                this.$scope.detail.endAreaCode,
                this.$scope.detail.tonnageRange,
                this.$scope.detail.price,
                this.$scope.detail.priceUnit,
                this.$scope.detail.goodsTypeId,
                this.$scope.detail.loadingType,
                this.$scope.detail.carLength,
                this.$scope.detail.setleWay,
                this.$scope.detail.priceType,
                this.$scope.detail.mileage,
                this.$scope.detail.priceState,
                //this.$scope.detail.settleName,
                $("#settleContact_value").val(),
                this.$scope.detail.settleContact,
                this.$scope.detail.settlePhoneNumber,
                this.$scope.detail.remarks
            ).then(result => {
                this.reload();
                $('#lineprice').modal('hide');
            });
        });
    }

    showEditLinePriceDialog = (id: string) => {
        this.$scope.ischecked = true;
        this.$scope.title = "编辑线路报价";
        $('#opendataTime').click(function () {
            $('#dataTime').datetimepicker('show');
        });
        $('#opendataTimeEnd').click(function () {
            $('#dataTimeEnd').datetimepicker('show');
        });
        var validator = $('#lineProceForm').validate();
        validator.form();
        validator.hideErrors();
        $(".error").removeClass("error");
        this.linePriceService.getDetail(id).then(result => {
            let d = result.data;
            this.goodsService.getList(this.enterpriseId, "", d.goodsTypeId, 0, -1).then((res) => {
                this.$scope.goodsNameList = res.data.data;
                this.$scope.detail.goodsName = d.goodsId;
            });
            // this.$scope.detail.goodsName = d.goodsId;
            this.areaService.getCity(d.startProvince).then(res => {
                this.$scope.startCityList = res.data.data;
            });
            this.areaService.getCounty(d.startCity).then(res => {
                this.$scope.startAreaList = res.data.data;
            })
            this.areaService.getCity(d.endProvince).then(res => {
                this.$scope.endCityList = res.data.data;
            });
            this.areaService.getCounty(d.endCity).then(res => {
                this.$scope.endAreaList = res.data.data;
            })
            this.$scope.detail = {
                id: id,
                tonnageRange: d.tonnageRange,
                price: d.price,
                priceUnit: d.priceUnit,
                goodsTypeId: d.goodsTypeId,
                loadingType: d.loodingType,
                carLength: d.carLength,
                setleWay: d.setleWay,
                priceType: d.priceType,
                mileage: d.mileage,
                priceState: d.priceState,
                settleName: d.settleName,
                // settleName:$("#settleContact_value").val(d.settleName),
                settleContact: d.settleContact,
                settlePhoneNumber: d.settlePhone,
                remarks: d.remarks,
                click: this.editLinePrice,
                openTime: d.openTime,
                closeTime: d.closeTime,
                startProvinceCode: d.startProvince,
                startCityCode: d.startCity,
                startAreaCode: d.startArea,
                endProvinceCode: d.endProvince,
                endCityCode: d.endCity,
                endAreaCode: d.endArea,
                goodsId: d.goodsId
            };
            $("#settleContact_value").val(d.settleName);
        });
    }

    editLinePrice = () => {
        this.$ngBootbox.confirm("确定保存此数据吗？").then(e => {
            this.linePriceService.edit(
                this.$scope.detail.id,
                this.enterpriseId,
                this.$scope.detail.openTime,
                this.$scope.detail.goodsId,
                this.$scope.detail.closeTime,
                this.$scope.detail.startProvinceCode,
                this.$scope.detail.startCityCode,
                this.$scope.detail.startAreaCode,
                this.$scope.detail.endProvinceCode,
                this.$scope.detail.endCityCode,
                this.$scope.detail.endAreaCode,
                this.$scope.detail.tonnageRange,
                this.$scope.detail.price,
                this.$scope.detail.priceUnit,
                this.$scope.detail.goodsTypeId,
                this.$scope.detail.loadingType,
                this.$scope.detail.carLength,
                this.$scope.detail.setleWay,
                this.$scope.detail.priceType,
                this.$scope.detail.mileage,
                this.$scope.detail.priceState,
                //this.$scope.detail.settleName,
                $("#settleContact_value").val(),
                this.$scope.detail.settleContact,
                this.$scope.detail.settlePhoneNumber,
                this.$scope.detail.remarks
            ).then(result => {
                this.reload();
                $('#lineprice').modal('hide');
            });
        });
    }

    deleteLinePrice = (id: string) => {
        this.$ngBootbox.confirm("确定删除此数据吗？").then(e => {
            this.linePriceService.delete(id).then(result => {
                this.reload();
            });
        });
    }

}

angular.module("tms").controller("linePriceManagentController", ["$scope", "linePriceService", "settleService", "areaService", "goodsService", "goodsTypeService", "valueService", "$ngBootbox", "$location", LinePriceManagentController]);